package formservlet;
import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreAttributes;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import java.io.IOException;
import java.util.Iterator;

//import guestbook.PMF;

public class SignInServlet extends HttpServlet  {

	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		getServletContext().getRequestDispatcher("top.jsp").forward(req, resp);
	}
 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println("You Have Entered username: " + username + " password " + password);
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

		try {
			Key key = KeyFactory.createKey("UserSignUp", username);
			Entity e=datastore.get(key);
			System.out.print(e.getProperties());
			String pwd=e.getProperty("Password").toString();
			if(pwd.equalsIgnoreCase(password))
			{
				response.sendRedirect("SignInPage.html");
			}
			else
			{
				response.sendRedirect("UnsuccessfulSignIn.html");
			}
		} catch (EntityNotFoundException e) {
			// TODO Auto-generated catch block
			response.sendRedirect("UnsuccessfulSignIn.html");
			e.printStackTrace();
		}
		response.sendRedirect("index.html");
 
	}
 
}
